#pragma once
#include <pthread.h>

namespace MutexModule
{
    class Mutex
    {
    public:
        Mutex()
        {
            int n = pthread_mutex_init(&_lock, nullptr);
            (void)n;
        }
        void Lock()
        {
            int n = pthread_mutex_lock(&_lock);
            (void)n;
        }
        void UnLock()
        {
            int n = pthread_mutex_unlock(&_lock);
            (void)n;
        }
        ~Mutex()
        {
            int n = pthread_mutex_destroy(&_lock);
            (void)n;
        }
        pthread_mutex_t* getLock()
        {
            return &_lock;
        }
    private:
        pthread_mutex_t _lock;
    };

    // RAII风格的互斥锁的实现
    class LockGuard
    {
    public:
        LockGuard(Mutex& mutex)
            : _mutex(mutex)
        {
            _mutex.Lock();
        }
        ~LockGuard()
        {
            _mutex.UnLock();
        }

    private:
        Mutex _mutex;
    };
}
